﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCExtensions;

namespace Cost.Controllers
{
    public class InvoiceManageController : Controller
    {
         #region 构造函数
        private readonly ICostRepository<InvoiceManage> invoiceManageRepository;

        public InvoiceManageController(ICostRepository<InvoiceManage> invoiceManageRepository)
        {
            this.invoiceManageRepository = invoiceManageRepository;
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(string Id)
        {
            var data = invoiceManageRepository.Find(Id) ?? new InvoiceManage();
            data.Date = DateTime.Now;
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(string Id)
        {
            var data = invoiceManageRepository.Find(Id) ?? new InvoiceManage();
            TryUpdateModel(data);
            invoiceManageRepository.Save(data);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult Delete(string Id)
        {
            invoiceManageRepository.Delete(Id);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult DeleteAll(string Ids)
        {
            var items = Ids.Split(',');
            invoiceManageRepository.DeleteAll(d => items.Contains(d.Id));
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult List(string IsFilter, nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = invoiceManageRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
    }
}
